package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.bean.Work;
import com.naughty.userlogin02.dao.WorkDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class WorkController {
    @Autowired
    WorkDao workDao;

    @CrossOrigin
    @RequestMapping("/allWork")
    public String getWorkList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        //
        int numbers = workDao.getWorkCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数，此处为模糊查询
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Work> work = workDao.getAllWork("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",work);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @RequestMapping("/addWork")
    public String addWork(@RequestBody Work work){
        System.out.println(work);
        int i = workDao.addWork(work);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdateWork")
    public String getUpdateWork(String wid){
        System.out.println("编号:"+wid);
        Work updateWorks = workDao.getUpdateWork(wid);
        String users_json = JSON.toJSONString(updateWorks);
        return users_json;
    }

    @RequestMapping("/editWork")
    public String editWork(@RequestBody Work work){
        System.out.println(work);
        int i = workDao.editWork(work);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteWork")
    public String deleteWork(String wid){
        System.out.println(wid);
        int i = workDao.deleteWork(wid);
        String str = i >0?"success":"error";
        return str;
    }


}